package com.gamebox.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;

import sun.management.snmp.util.JvmContextFactory;

import com.gamebox.dao.GameDao;
import com.gamebox.javaBean.Game;
import com.gamebox.javaBean.User;

public class GameBoxFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5561064697273575893L;
	private GameDao gameDao;
	private ArrayList<Game> games = new ArrayList<Game>();
	
	private User user;

	public GameBoxFrame(User loginUser) {
		this.setTitle("GameBox");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(800, 600);
		this.setLocation(this.getToolkit().getScreenSize().width / 2 - this.getWidth() / 2, this
				.getToolkit().getScreenSize().height / 2 - this.getHeight() / 2);
		this.setLayout(null);
		
		this.user = loginUser;

		gameDao = new GameDao();
		games = gameDao.getAllGames(user);

		this.setLayout(new BorderLayout());
		final GameTable gameTable = new GameTable(games);
		gameTable.setUser(user);
		final JTable t = gameTable.getTable();
		JScrollPane jScrollPane = new JScrollPane(t);

		GameBoxFrame.this.getContentPane().add(jScrollPane, BorderLayout.CENTER);
		JPanel btPanel = new JPanel();
		JButton jb = new JButton("删除");
		btPanel.add(jb);
		jb.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int i = t.getSelectedRow();
				String name = (String) t.getValueAt(i, 0);
				Game game = gameDao.getGamesByName(name).get(0);
				gameDao.deleteSaveRecord(user.getId(), game.getId());
				GameBoxFrame.this.games = gameDao.getAllGames(user);
				gameTable.setGames(GameBoxFrame.this.games);
			}
		});
		GameBoxFrame.this.getContentPane().add(btPanel,BorderLayout.SOUTH);
		GameBoxFrame.this.repaint();
		GameBoxFrame.this.validate();

		JMenu fileMenu = new JMenu("文件");
		JMenuItem importItem = new JMenuItem("导入");
		importItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JFileChooser jf = new JFileChooser("选择jar文件");
				jf.setFileFilter(new FileFilter() {

					@Override
					public String getDescription() {
						// TODO Auto-generated method stub
						return "jar files";
					}

					@Override
					public boolean accept(File f) {
						// TODO Auto-generated method stub
						if (f.isDirectory())
							return true;
						String name = f.getName();
						if (name.endsWith(".jar")) {
							return true;
						}
						return false;
					}
				});
				FileSystemView fsv = FileSystemView.getFileSystemView(); 
				jf.setCurrentDirectory(fsv.getHomeDirectory());

				jf.setDialogTitle("选择jar包");
				int result = jf.showOpenDialog(GameBoxFrame.this);
				jf.setVisible(true);
				if (result == JFileChooser.APPROVE_OPTION) {
					File selectedFile = jf.getSelectedFile();
					String path = selectedFile.getAbsolutePath();
					String name = selectedFile.getName();
					name = name.substring(0, name.length()-4);
					List<Game> temp = gameDao.getGamesByName(name);
					Game game = null;
					if(temp.size()>0){
						game = temp.get(0);
						gameDao.addSaveRecord(GameBoxFrame.this.user.getId(),game.getId() );
					}else{
						String label = JOptionPane.showInputDialog("请输入标签");
						game = new Game(0, name, label, path, 0,0,0);
						int gid = gameDao.addGame(game);
						game.setId(gid);
						gameDao.addSaveRecord(GameBoxFrame.this.user.getId(),gid );
					}
					games.add(game);
					gameTable.setGames(games);
					GameBoxFrame.this.repaint();
					GameBoxFrame.this.validate();
					
				}
			}
		});
		fileMenu.add(importItem);
		
		JMenuItem sortItem = new JMenuItem("导入数据并排序");
		sortItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				GameBoxFrame.this.games = gameDao.getAllGames(user);
				gameTable.setGames(GameBoxFrame.this.games);
			}
		});
		fileMenu.add(sortItem);
		
		JMenu searchMenu = new JMenu("搜索");
		JMenuItem nameSearchItem = new JMenuItem("按游戏名搜索");
		nameSearchItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String name = JOptionPane.showInputDialog("请输入游戏名");
				ArrayList<Game>games = gameDao.getFuzzyGamesByName(name,user);
				GameBoxFrame.this.games = games;
				gameTable.setGames(games);
			}
		});
		searchMenu.add(nameSearchItem);
		
		JMenuItem labelSearchItem = new JMenuItem("按标签进行搜索");
		labelSearchItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String name = JOptionPane.showInputDialog("请输入标签名");
				ArrayList<Game>games = gameDao.getGamesByLabel(name,user);
				GameBoxFrame.this.games = games;
				gameTable.setGames(games);
			}
		});
		searchMenu.add(labelSearchItem);
		
		JMenu friendMenu = new JMenu("好友");
		JMenuItem myFri = new JMenuItem("我的好友");
		myFri.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				JDialog friendDia = new JDialog();
				friendDia.setTitle("我的好友");
				List<String> s = gameDao.getMyFriends(user.getId());
				JList flist = new JList(s.toArray());
				friendDia.add(flist);
				friendDia.setBounds(400, 200,200, 400);
				friendDia.setVisible(true);
			}
		});
		JMenuItem talk = new JMenuItem("对话记录");
		talk.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				JDialog talkDia = new JDialog();
				talkDia.setTitle("对话记录");
				List<String> s = gameDao.getMyTalk(user.getId());
				final JList flist = new JList(s.toArray());
				flist.addMouseListener(new MouseAdapter(){
					public void mouseClicked(MouseEvent e){
						if(e.getClickCount()==2){	
							String s = JOptionPane.showInputDialog("请输入回复内容:");
							String ss = (String) flist.getSelectedValue();
							String nick = ss.split(" ")[0];
							User user1 = gameDao.getUserByNick(nick);
							int from = user.getId();
							int to = user1.getId();
							gameDao.addTalk(from, to, s);
							flist.setListData(gameDao.getMyTalk(user.getId()).toArray());
						}
					}
				});
				talkDia.add(flist);
				talkDia.setBounds(400, 200,300, 400);
				talkDia.setVisible(true);
			}
		});
		JMenuItem commonFri = new JMenuItem("查看共同好友");	
		commonFri.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				final JDialog comDia = new JDialog();
				comDia.setTitle("查看共同好友");
				JLabel jl = new JLabel("选择好友");
				comDia.getContentPane().setLayout(null);
				jl.setBounds(10, 10, 70, 30);
				comDia.getContentPane().add(jl);
				final JComboBox com = new JComboBox(gameDao.getMyFriends(user.getId()).toArray());
				com.setBounds(100, 10, 100, 30);
				comDia.getContentPane().add(com);
				JButton confirm = new JButton("查看");
				confirm.setBounds(250, 10, 90, 30);
				final JList  flist = new JList();
				flist.setBounds(10, 60, 200, 200);
				comDia.getContentPane().add(flist);
				confirm.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						List<String> s = gameDao.getComFri(user.getId(),(String)com.getSelectedItem());
						flist.setListData(s.toArray());
						comDia.repaint();
					}
				});
				comDia.getContentPane().add(confirm);
				comDia.setBounds(400, 200,400, 400);
				comDia.setVisible(true);
			}
		});
		
		JMenuItem frireq = new JMenuItem("好友请求");	
		frireq.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				final JDialog frireqDia = new JDialog();
				frireqDia.setTitle("好友请求");
				frireqDia.getContentPane().setLayout(null);
				final List<String> strList = gameDao.getFriendReq(user.getId());
				if(strList.size()>0){
					JLabel jl = new JLabel(strList.get(0));
					jl.setBounds(100, 40, 120, 30);
					JButton jb = new JButton("通过");
					jb.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							// TODO Auto-generated method stub
							String nickName = strList.get(0).split(" ")[0];
							User fuser = gameDao.getUserByNick(nickName);
							gameDao.updateFriend(fuser.getId(),user.getId());
							gameDao.addFriend2(user.getId(), fuser.getId());
							JOptionPane.showMessageDialog(null, "添加好友成功");
							frireqDia.dispose();
						}
					});
					jb.setBounds(100, 80, 80, 30);
					JButton jb2 = new JButton("拒绝");
					jb2.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							// TODO Auto-generated method stub
							frireqDia.dispose();
						}
					});
					jb2.setBounds(190, 80, 80, 30);
					frireqDia.getContentPane().add(jl);
					frireqDia.getContentPane().add(jb);
					frireqDia.getContentPane().add(jb2);
				}
				
				frireqDia.setBounds(400, 200,400, 200);
				frireqDia.setVisible(true);
			}
		});
		JMenuItem opentalk= new JMenuItem("发起对话");	
		opentalk.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				final JDialog talk = new JDialog();
				talk.setTitle("发起对话");
				talk.getContentPane().setLayout(null);
				JLabel jl = new JLabel("选择好友");
				jl.setBounds(10, 10, 100, 30);
				final JComboBox com = new JComboBox(gameDao.getMyFriends(user.getId()).toArray());
				com.setBounds(120, 10, 100, 30);
				
				JLabel jl2 = new JLabel("输入信息");
				jl2.setBounds(10, 50, 100, 30);
				final JTextField text = new JTextField();
				text.setBounds(120, 50, 100, 30);
				
				JButton jb2 = new JButton("发送");
				jb2.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						int fromId = user.getId();
						String nick = (String) com.getSelectedItem();
						User toUser = gameDao.getUserByNick(nick);
						int toId = toUser.getId();
						String content = text.getText();
						gameDao.addTalk(fromId,toId,content);
						JOptionPane.showMessageDialog(null, "发送成功");
					}
				});
				jb2.setBounds(120, 100, 100, 30);
				talk.getContentPane().add(jl);
				talk.getContentPane().add(jb2);
				talk.getContentPane().add(com);
				talk.getContentPane().add(jl2);
				talk.getContentPane().add(text);
				talk.setBounds(400, 200,400, 200);
				talk.setVisible(true);
			}
		});
		
		JMenu infoMenu = new JMenu("个人信息");
		JMenuItem infoMe = new JMenuItem("查看个人信息");
		infoMe.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				new InfoDialog(user); 
			}
			
		});
		JMenuItem repairMe = new JMenuItem("修改个人信息");
		repairMe.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				new ChangeInfoDialog(user);
			}
			
		});
		JMenuItem giveup = new JMenuItem("注销账号");
		giveup.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int d = JOptionPane.showConfirmDialog(null, "您确定要注销账号吗？","",JOptionPane.YES_NO_CANCEL_OPTION);
				 if(d==JOptionPane.YES_OPTION){
					 gameDao.deleteUser(user.getId());
					 JOptionPane.showMessageDialog(null, "注销账号成功");
					 GameBoxFrame.this.dispose();
				 }
			}
			
		});
		infoMenu.add(infoMe);
		infoMenu.add(repairMe);
		infoMenu.add(giveup);
		
		friendMenu.add(myFri);
		friendMenu.add(talk);
		friendMenu.add(commonFri);
		friendMenu.add(frireq);
		friendMenu.add(opentalk);
		
		JMenuBar bar = new JMenuBar();
		bar.add(fileMenu);
		bar.add(searchMenu);
		bar.add(friendMenu);
		bar.add(infoMenu);
		setJMenuBar(bar);
	}
}
